package com.twinkle.student;

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;

public class StuDemo {
    /**
     * 学生管理系统
     */
    static int count = 2;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isExit = false;
        String[][] datas = new String[100][];//存储学生信息
        datas[0] = new String[]{"S001", "张三", "20"};
        datas[1] = new String[]{"S002", "李四", "21"};

        do {
            //打印一级菜单
            printFirstMenu();
            //接受输入
            String firstChoice = sc.nextLine();
            //响应输入
            //使用正则表达式进行异常处理
            switch (Pattern.matches("^[1-9]\\d*$", firstChoice) ? Integer.parseInt(firstChoice) : -1) {
                case 1 -> {
                    System.out.println("-- 进入登录");
                    loginVerify();
                    System.out.println("-- 进入二级菜单！");
                    boolean isExitSecondMenu = false;
                    do {
                        printSecondMenu();
                        String secondChoice = sc.nextLine();
                        //使用正则表达式进行异常处理
                        switch (Pattern.matches("^[1-9]\\d*$", secondChoice) ? Integer.parseInt(secondChoice) : -1) {
                            case 1 -> {
                                System.out.println("新增");
                                insertStudent(datas);
                            }
                            case 2 -> {
                                System.out.println("修改");
                                updateStudent(datas);
                            }
                            case 3 -> {
                                System.out.println("删除");
                                deleteStudent(datas);
                            }
                            case 4 -> {
                                System.out.println("查询");
                                selectStudent(datas);
                            }
                            case 5 -> {
                                System.out.println("显示所有");
                                showAllStudent(datas);
                            }
                            case 6 -> isExitSecondMenu = true;
                            default -> System.out.println("无效输入");
                        }
                    } while (!isExitSecondMenu);
                }
                case 2 -> isExit = true;
                default -> System.out.println("-- 输入无效");
            }
        } while (!isExit);

        sc.close();
    }

    /**
     * 打印一级菜单
     */
    private static void printFirstMenu() {
        System.out.println("-- 欢迎使用学生信息管理系统");
        System.out.println("1.登录");
        System.out.println("2.退出");
    }

    /**
     * 打印二级菜单
     */
    private static void printSecondMenu() {
        System.out.println("-- 请选择操作：");
        System.out.println("1.新增");
        System.out.println("2.修改");
        System.out.println("3.删除");
        System.out.println("4.查询");
        System.out.println("5.显示所有");
        System.out.println("6.退出");
    }

    /**
     * 登录校验
     */
    private static void loginVerify() {
        Scanner loginScanner = new Scanner(System.in);
        do {
            System.out.print("输入用户名：");
            String username = loginScanner.nextLine();
            System.out.print("输入密码：");
            String password = loginScanner.nextLine();
            if ("QST".equals(username) && "123456".equals(password)) {
                break;
            } else {
                System.out.println("-- 用户名或密码错误！");
            }
        } while (true);
    }

    /**
     * 遍历所有学生
     */
    private static void showAllStudent(String[][] students) {
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < students[i].length; j++) {
                System.out.print(students[i][j] + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 返回索引
     *
     * @param students 源数据
     * @param no       待查询序号
     * @return index 查询到的数据的索引
     */
    private static int queryStudentByNo(String[][] students, String no) {
        int index = -1;
        for (int i = 0; i < count; i++) {
            if (no.equals(students[i][0])) {
                index = i;
                return index;
            }
        }
        return index;
    }

    /**
     * 增加学生
     *
     * @param students 源数据
     */
    private static void insertStudent(String[][] students) {
        Scanner insertScanner = new Scanner(System.in);
        String stuNo = null;
        do {
            System.out.print("请输入新学号：");
            stuNo = insertScanner.nextLine();
        } while (queryStudentByNo(students, stuNo) != -1);
        System.out.print("请输入姓名：");
        String stuName = insertScanner.nextLine();
        System.out.print("请输入年龄：");
        String stuAge = insertScanner.nextLine();
        students[count++] = new String[]{stuNo, stuName, stuAge};
    }

    /**
     * 修改学生
     *
     * @param students 源数据
     */
    private static void updateStudent(String[][] students) {
        Scanner updateScanner = new Scanner(System.in);
        String stuNo = null;
        int oldStuIndex = -1;
        do {
            System.out.print("请输入需要修改的学生学号：");
            stuNo = updateScanner.nextLine();
            oldStuIndex = queryStudentByNo(students, stuNo);
        } while (oldStuIndex == -1);
        System.out.print("请输入姓名：");
        String stuName = updateScanner.nextLine();
        System.out.print("请输入年龄：");
        String stuAge = updateScanner.nextLine();
        students[oldStuIndex] = new String[]{stuNo, stuName, stuAge};
    }

    /**
     * 删除学生
     *
     * @param students 源数据
     */
    private static void deleteStudent(String[][] students) {
        Scanner deleteScanner = new Scanner(System.in);
        String stuNo = null;
        int oldStuIndex = -1;
        do {
            System.out.print("请输入需要删除的学生学号：");
            stuNo = deleteScanner.nextLine();
            oldStuIndex = queryStudentByNo(students, stuNo);
        } while (oldStuIndex == -1);
        for (int i = 0; i < count - 1; i++) {
            if (i >= oldStuIndex) {
                students[i] = students[i + 1];
            }
        }
        count--;
    }

    /**
     * 查询学生
     *
     * @param students 源数据
     */
    private static void selectStudent(String[][] students) {
        Scanner selectScanner = new Scanner(System.in);
        String stuNo = null;
        int oldStuIndex = -1;
        do {
            System.out.print("请输入需要查询的学生学号：");
            stuNo = selectScanner.nextLine();
            oldStuIndex = queryStudentByNo(students, stuNo);
        } while (oldStuIndex == -1);
        System.out.println(Arrays.toString(students[oldStuIndex]));
    }

}
